package delegate

import kotlin.reflect.KProperty

class Example {
    var p: String by Delegate()
}

class Delegate {
    private var p = "123"
    operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
        println("$thisRef, 这里委托了 ${property.name} 属性")
        return p
    }

    operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
        println("$thisRef 的 ${property.name} 属性赋值为 $value")
//        if (value.toInt() < 100) {
//            throw Exception("${value.toInt()} need >= 100!")
//        }
        p = value
    }
}

fun main() {
    val e = Example()
    println(e.p)

    e.p = "50"
    println(e.p)
}